package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.util.RxToast

/**
 * @author:hxw
 * @date  :2022/12/8
 * @desc  :
 */
class RxInputRingDialog(context: Context?) : RxDialog(context!!) {

    private lateinit var cancelView: AppCompatTextView
    private lateinit var sureView: AppCompatTextView
    private lateinit var titleView: AppCompatTextView
    private lateinit var tvInputRingYear: AppCompatEditText
    private lateinit var tvInputRingArea: AppCompatEditText
    private lateinit var tvInputRing: AppCompatEditText

    init {
        initView()
    }

    @SuppressLint("MissingInflatedId")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_input_ring, null)
        cancelView = dialogView.findViewById(R.id.tvCancel)
        sureView = dialogView.findViewById(R.id.tvSure)
        titleView = dialogView.findViewById(R.id.tvtDialogTitle)
        tvInputRingYear = dialogView.findViewById(R.id.tvInputRingYear)
        tvInputRingArea = dialogView.findViewById(R.id.tvInputRingArea)
        tvInputRing = dialogView.findViewById(R.id.tvInputRing)

        cancelView.setOnClickListener {
            dismiss()
        }
        setContentView(dialogView)
    }


    fun getContent(): String {
        val yearRing = tvInputRingYear.text.toString().trim()
        val tvInputRingArea = tvInputRingArea.text.toString().trim()
        val tvInputRing = tvInputRing.text.toString().trim()
        if (yearRing.isEmpty()) {
            RxToast.warning(context.getString(R.string.enter_pigeon_year))
            return ""
        } else if (tvInputRing.isEmpty()) {
            RxToast.warning(context.getString(R.string.Please_fill_in_the_love_pigeon_ring_number))
            return ""
        } else {
            val are = tvInputRingArea.ifEmpty { "00" }
            return "$yearRing-$are-$tvInputRing"
        }
    }

    fun setRingData(ring: String) {
        if (ring.isNotEmpty() && ring.contains("-")) {
            val result = ring.split("-")
            tvInputRingYear.setText(result[0])
            tvInputRingArea.setText(result[1])
            tvInputRing.setText(result[2])
        }
    }

    fun setSureListener(listener: View.OnClickListener?) {
        sureView.setOnClickListener(listener)
    }

    fun setTitle(titleStr: String) {
        titleView.text = titleStr
    }
}